package editalias;

## This file is part of the Simplemail project.
#  Copyright (c) 2008 the Simplemail Team (see authors).
# 
#  For the full copyright and license information, please view the COPYING
#  file that was distributed with this source code. You can also view the
#  COPYING file online at http://unixforum.org.ua/misc/COPYING.txt
# 
#  @copyright  (c) the authors
#  @author     Dmitriy Shilenko <rtyug@ukr.net>
#  @license    GPLv2 http://unixforum.org.ua/misc/COPYING.txt
#  @package    Panel Mailbox
#  @version    $Id: $

use strict;
use CGI::Carp qw(fatalsToBrowser);
use Data::Validate::Domain qw(is_domain);
use Data::Validate::Email qw(is_email);
use base 'func';

sub new {
    my $self = shift->SUPER::new(@_);
    $self->dbi_connect if $self->can("dbi_connect");
    return $self;
}

sub return_edit_alias2 {
    my $self = shift;

    my %he = ();

    if (   ( !length( $self->{p}->{address} ) )
        || ( 250 < length( $self->{p}->{address} ) ) )
    {
        $self->{he}->{param_vaild} = 1;
    }

    if ( !$self->{p}->{fGoto} ) {
        $self->{he}->{no_goto} = 1;
    }

    my @is_goto;
    my $index;
    if ( $he{no_goto} != 1 ) {
        @is_goto = split ' ', $self->{p}->{fGoto};
        my ( $found, $item );
        foreach $item (@is_goto) {

            if ( !is_email($item) ) {
                $found = 1;
                $index = $item;
                last;
            }
        }
        $self->{he}->{valid_error} = $index if ( defined($index) );

        if ($found) {
            $self->{he}->{no_valid_goto} = 1;
        }

    }

    my $rows;
    my $goto_in;
    if ( $self->{he}->{no_goto} != 1 ) {

        my @sql = ();
        my $cl;
        if ( $self->{session}->{name} eq 'Admin' ) {
            push @sql, $self->{p}->{address};
        }
        else {
            push @sql, $self->{p}->{address}, $self->{session}->{user};
            $cl = 'AND t1.idnane = ?';
        }

        my $sth = $self->{dbh}->prepare(
            "SELECT t1.aliases,
                                  t2.address
                         FROM domain AS t1,
                              alias AS t2
                         WHERE t1.domain = t2.domain
                         AND t2.address = ?
                          $cl                       
                              "
        );
        $sth->execute(@sql);

        $rows = $sth->fetchrow_hashref();
        $sth->finish();

        if ( $rows->{aliases} == -1 ) {
            $self->{he}->{alias_disable} = 1;
        }

        $goto_in = join( ',', @is_goto );
    }

    my ( $value, $found );
    foreach my $value ( values( %{ $self->{he} } ) ) {
        if ( $value == 1 ) { $found = 1; last; }
    }
    if ( !$found ) {

        my $sth = $self->{dbh}->prepare( '
                            UPDATE alias
                            SET goto = ?,
                            modified = DATETIME("NOW") 

                            WHERE address = ?
              ' );

        $sth->execute( $goto_in, $self->{p}->{address} );
        $sth->finish();
        $self->{he}->{alias_edit} = 1;
    }

    return $self;
}

sub return_edit_alias {

    my $self = shift;

    if (   ( !length( $self->{p}->{address} ) )
        || ( 250 < length( $self->{p}->{address} ) )
        || ( !length( $self->{p}->{domain} ) )
        || ( 250 < length( $self->{p}->{domain} ) ) )
    {
        $self->{he}->{param_vaild} = 1;
    }

    my $rows;

    if ( $self->{he}->{param_vaild} != 1 ) {
        my @sql = ();
        my $cl;
        if ( $self->{session}->{name} eq 'Admin' ) {
            push @sql, $self->{p}->{address};
        }
        else {
            push @sql, $self->{p}->{address}, $self->{session}->{user};
            $cl = 'AND t1.idnane = ?';
        }

        my $sth = $self->{dbh}->prepare(
            "SELECT t1.aliases,
                                t2.address,
                                t2.goto, 
                                t2.domain
                         FROM domain AS t1,
                              alias AS t2
                         WHERE t1.domain = t2.domain
                         AND t2.address = ?
                          $cl                       
                              "
        );
        $sth->execute(@sql);

        $rows = $sth->fetchrow_hashref();
        $sth->finish();
    }

    if ( $rows->{aliases} == -1 && $self->{session}->{name} ne 'Admin' ) {
        $self->{he}->{alias_disable} = 1;
    }

    if ( !$rows || !defined( $rows->{address} ) || !defined( $rows->{goto} ) ) {
        $self->{he}->{no_found} = 1;
    }

    my @goto_array = ();

    @goto_array = split( /,/, $rows->{goto} );
    @goto_array = map +{ goto => $_ }, @goto_array;

    $self->{he}->{ROWS} = \@goto_array;

    $self->{he}->{address} = $rows->{address};
    $self->{he}->{domain}  = $rows->{domain};

    return $self;
}

sub templ_hash_edit_alias {
    my $self = shift;

    $self->{session}->{filet} = 'edit-alias';
    $self->SUPER::templ_hash(@_);
}

sub DESTROY {
    my $self = shift;
    $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
}

1;
